/**
 * Created by alan on 2017/5/5.
 * 菜单管理
 */


Ext.define('Admin.view.waterDispath.RoleController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.roleController',

    init: function() {

    },

    //steps下一步
    check: function() {
        var roleGrid = this.lookupReference('roleGrid');
        var store = roleGrid.getStore();
        var name = this.lookupReference('name').getValue();
        store.getProxy().extraParams = { name: name, river: config.RIVER };
        store.reload();
    },
    //新增角色
    addrole: function() {
        var _that = this;
        var win = this.lookupReference('roleWindow');

        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增角色',
                width: 530,
                layout: 'fit',
                autoScroll: true,
                closeAction: 'close',
                reference: 'roleWindow',
                items: {
                    xtype: 'roleWindowForm',
                    border: false,
                    reference: 'roleUpdataForm'
                },
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('roleUpdataForm').getForm();
                            var valuesa = form.getValues();


                            var checkboxtree = _that.lookupReference('roleUpdataForm').items.items[3].items.items[1].items.items[0];

                            var treestore = checkboxtree.store;
                            var allroledata = [];

                            for (var key in treestore.byIdMap) {
                                if (treestore.byIdMap[key].data.done) {
                                    allroledata.push(treestore.byIdMap[key].data.id)
                                }
                            }

                            if (form.isValid()) {
                                var modal = this.up('window');
                                var roleGrid = _that.lookupReference('roleGrid');
                                var store = roleGrid.getStore();

                                valuesa.river = config.RIVER;
                                valuesa.menuList = allroledata;

                                delete valuesa.id;

                                Ext.Ajax.request({
                                    url: config.URL + 'szy/sys/role/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        Ext.MessageBox.hide();
                                        var result = Ext.JSON.decode(response.responseText);

                                        Ext.Msg.alert('系统提示', "保存成功");
                                        store.reload();
                                        modal.close();

                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: valuesa
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('roleUpdataForm').getForm().reset();
                            modal.close()
                        }
                    }
                ],

            });
            this.getView().add(win);
        }
        var form = _that.lookupReference('roleUpdataForm').getForm();
        form.reset();
        var checkboxtree = _that.lookupReference('roleUpdataForm').items.items[3].items.items[1].items.items[0];
        var treestore = checkboxtree.getStore();
        treestore.load({
            scope: this,
            callback: function(records, operation, success) {
                treestore.byIdMap['root'].data.done = true;
                checkboxtree.reconfigure(treestore);

            }
        });


        win.show()

    },
    //修改角色信息
    update: function(grid, rowIndex, colIndex) {
        var _that = this;
        var rec = grid.getStore().getAt(rowIndex);
        var win = this.lookupReference('roleWindow');

        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改角色信息',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'close',
                reference: 'roleWindow',
                items: {
                    xtype: 'roleWindowForm',
                    border: false,
                    reference: 'roleUpdataForm'
                },
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('roleUpdataForm').getForm();
                            var values = form.getValues();
                            var checkboxtree = _that.lookupReference('roleUpdataForm').items.items[3].items.items[1].items.items[0];

                            var treestore = checkboxtree.store;
                            var allroledata = [];
                            for (var key in treestore.byIdMap) {
                                if (treestore.byIdMap[key].data.done) {
                                    allroledata.push(treestore.byIdMap[key].data.id)
                                }
                            }

                            if (form.isValid()) {
                                var modal = this.up('window');
                                var roleGrid = _that.lookupReference('roleGrid');
                                var store = roleGrid.getStore();

                                values.river = config.RIVER;
                                values.menuList = allroledata;
                                Ext.Ajax.request({
                                    url: config.URL + 'szy/sys/role/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        Ext.MessageBox.hide();
                                        var result = Ext.JSON.decode(response.responseText);

                                        Ext.Msg.alert('系统提示', "修改成功");
                                        store.reload();
                                        modal.close();

                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('roleUpdataForm').getForm().reset();
                            modal.close();
                        }
                    }
                ],

            });
            this.getView().add(win);
        }
        var form = _that.lookupReference('roleUpdataForm').getForm();
        form.reset();
        form.setValues(rec.data);
        var checkboxtree = _that.lookupReference('roleUpdataForm').items.items[3].items.items[1].items.items[0];
        console.log(checkboxtree);
        var treestore = checkboxtree.getStore();

        treestore.load({
            scope: this,
            callback: function(records, operation, success) {
                var roledata = [];

                Ext.Ajax.request({
                    url: config.URL + "szy/sys/role/get?id=" + rec.id,
                    method: "get",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.menuList) {
                            roledata = result.menuList; //角色权限菜单
                        }
                        treestore.byIdMap['root'].data.done = true;
                        for (var i = 0; i < roledata.length; i++) {
                            for (var key in treestore.byIdMap) {
                                if (roledata[i].id == key && key != 'root') {
                                    treestore.byIdMap[key].data.done = true;
                                }
                            }
                        }
                        checkboxtree.reconfigure(treestore);
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                });
            }
        });
        win.show()

    },

    //删除
    delete: function(grid, rowIndex, colIndex) {
        var _that = this;
        var rec = grid.getStore().getAt(rowIndex);

        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + 'szy/sys/role/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { id: rec.id, river: config.RIVER }
                });
            }
        })
    }


});